package demo10;

/**
 * @author JYH
 * @version 1.0
 * @date 2022/11/1 17:27
 */
public class DeadLockTest {
    public static void main(String[] args) {
        pen pen = new pen();
        board board = new board();

        pen.setName("A");
        board.setName("B");
        pen.start();
        board.start();
    }
}

class draw{
    public static Object lock1=new Object();
    public static Object lock2=new Object();
}
class pen extends Thread{
    @Override
    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (draw.lock1){
            System.out.println(Thread.currentThread().getName()+"获取画笔");
            synchronized (draw.lock2){
                System.out.println(Thread.currentThread().getName()+"获取画板");
                System.out.println(Thread.currentThread().getName()+"可以画画");
            }
        }

    }
}
class board extends Thread{
    @Override
    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (draw.lock2){
            System.out.println(Thread.currentThread().getName()+"获取画板");
            synchronized (draw.lock1){
                System.out.println(Thread.currentThread().getName()+"获取画笔");
                System.out.println(Thread.currentThread().getName()+"可以画画");
            }
        }

    }

}